package com.deer.wms.project.root.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.mapper.SuperMapper;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * @author : wei
 * @since : 2021-10-28 10:50:41
 **/
public class SuperServiceImpl<M extends SuperMapper<T>, T> extends ServiceImpl<M, T> implements SuperService<T> {

    private Class<T> entityClass = null;

    public SuperMapper getSuperMapper() {
        if (baseMapper instanceof SuperMapper) {
            return baseMapper;
        }
        throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "Mapper类转换异常");
    }

//    @Override
//    public Class<T> getEntityClass() {
//        if (entityClass == null) {
//            this.entityClass = (Class) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
//        }
//        return this.entityClass;
//    }

    @Override
    public T getOneByField(String field, Object value) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }

    @Override
    public T getOneByMap(Map<String, Object> map) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        return getOne(qw.allEq(map));
    }

    @Override
    public List<T> getListByField(String field, Object value) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        return list(qw.eq(field, value));
    }


    @Override
    public List<T> getOrderListByMap(Map<String, Object> map, Map<String, Boolean> map2) {
        QueryWrapper<T> qw = new QueryWrapper<>();
        for (Map.Entry<String, Boolean> entry : map2.entrySet()) {
            if(entry.getValue()){
                qw.orderByAsc(entry.getKey());
            }else{
                qw.orderByDesc(entry.getKey());
            }
        }
        return list(qw);
    }
}
